Trò chơi Halloween vui nhộn trong UNITY Engine
10.798 lượt xem;
- PlayerScript.cs
- PlayerScripts /
- Scripts /
- Assets /
- project /
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6 public class PlayerScript : MonoBehaviour {
7
8 public static PlayerScript instance;
9
10 [SerializeField]
11 public Rigidbody2D rb;
12
13 private float moveSpeed = 2f;
14 private float bounceSpeed = 4f;
15
16 private Button flapButton;
17
18 private bool didFlap;
19 public bool isAlive;
20
21 public GameObject explosion, impact;
22
23 public AudioSource audioSource;
24 public AudioClip scoreClip;
25
26 public GameObject[] scaryPics;
27 int randSCore;
28
29 void Awake () {
30
31 if (instance == null) {
32 instance = this;
33 }
34
35 isAlive = true;
36
37 flapButton = GameObject.FindGameObjectWithTag("FlapButton").GetComponent<Button>();
38 flapButton.onClick.AddListener(() => flapPlayer());
39
40 randSCore = Random.Range(20, 800);
41 }
42
43 public void flapPlayer() {
44 didFlap = true;
45 }
46
47 void FixedUpdate () {
48
49 if (isAlive) {
50 Vector3 temp = transform.position;
51 temp.x += moveSpeed * Time.deltaTime;
52 transform.position = temp;
53
54 if (didFlap) {
55 didFlap = false;
56 rb.velocity = new Vector2(0, bounceSpeed);
57 }
58 }
59
60 if (transform.position.y >= 5.6f) {
61 GameManager.instance.GameOver();
62 }
63 }
64
65 void CameraX() {
66 CameraMove.offSetX = (Camera.main.transform.position.x - transform.position.x);
67 }
68
69 public float GetPositionX() {
70 return transform.position.x;
71 }
72
73 void OnTriggerEnter2D(Collider2D collision) {
74 if (collision.tag == "Destruction") {
75 Destroy(this.gameObject);
76 GameManager.instance.GameOver();
77 }
78
79 if (collision.tag == "Ring") {
80 audioSource.PlayOneShot(scoreClip);
81 GameObject imp = (GameObject)Instantiate(impact, transform.position, transform.rotation);
82 Destroy(imp, 1f);
83 ScoreCount.instance.CountScore(1);
84
85 if (ScoreCount.instance.countScore >= randSCore) {
86 ScaryPictures();
87 }
88 }
89
90 if (collision.tag == "Death") {
91 Destroy(this.gameObject);
92 GameManager.instance.GameOver();
93 }
94
95 if (collision.tag == "Ground") {
96 Destroy(this.gameObject);
97 GameObject explode = (GameObject) Instantiate(explosion, transform.position, transform.rotation);
98 Destroy(explode, 0.8f);
99 GameManager.instance.GameOver();
100 }
101
102 if (collision.tag == "RingGroup") {
103 Destroy(collision.gameObject);
104
105 }
106 }
107
108 public void ScaryPictures() {
109 int rand = Random.Range(0, 5);
110 Vector3 picsPosition = new Vector3(transform.position.x, transform.position.y);
111 GameObject pics = (GameObject)Instantiate(scaryPics[rand], picsPosition, transform.rotation);
112 Destroy(pics, 2.5f);
113 }
114 }
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.UI;
5
6 public class PlayerScript : MonoBehaviour {
7
8 public static PlayerScript instance;
9
10 [SerializeField]
11 public Rigidbody2D rb;
12
13 private float moveSpeed = 2f;
14 private float bounceSpeed = 4f;
15
16 private Button flapButton;
17
18 private bool didFlap;
19 public bool isAlive;
20
21 public GameObject explosion, impact;
22
23 public AudioSource audioSource;
24 public AudioClip scoreClip;
25
26 public GameObject[] scaryPics;
27 int randSCore;
28
29 void Awake () {
30
31 if (instance == null) {
32 instance = this;
33 }
34
35 isAlive = true;
36
37 flapButton = GameObject.FindGameObjectWithTag("FlapButton").GetComponent<Button>();
38 flapButton.onClick.AddListener(() => flapPlayer());
39
40 randSCore = Random.Range(20, 800);
41 }
42
43 public void flapPlayer() {
44 didFlap = true;
45 }
46
47 void FixedUpdate () {
48
49 if (isAlive) {
50 Vector3 temp = transform.position;
51 temp.x += moveSpeed * Time.deltaTime;
52 transform.position = temp;
53
54 if (didFlap) {
55 didFlap = false;
56 rb.velocity = new Vector2(0, bounceSpeed);
57 }
58 }
59
60 if (transform.position.y >= 5.6f) {
61 GameManager.instance.GameOver();
62 }
63 }
64
65 void CameraX() {
66 CameraMove.offSetX = (Camera.main.transform.position.x - transform.position.x);
67 }
68
69 public float GetPositionX() {
70 return transform.position.x;
71 }
72
73 void OnTriggerEnter2D(Collider2D collision) {
74 if (collision.tag == "Destruction") {
75 Destroy(this.gameObject);
76 GameManager.instance.GameOver();
77 }
78
79 if (collision.tag == "Ring") {
80 audioSource.PlayOneShot(scoreClip);
81 GameObject imp = (GameObject)Instantiate(impact, transform.position, transform.rotation);
82 Destroy(imp, 1f);
83 ScoreCount.instance.CountScore(1);
84
85 if (ScoreCount.instance.countScore >= randSCore) {
86 ScaryPictures();
87 }
88 }
89
90 if (collision.tag == "Death") {
91 Destroy(this.gameObject);
92 GameManager.instance.GameOver();
93 }
94
95 if (collision.tag == "Ground") {
96 Destroy(this.gameObject);
97 GameObject explode = (GameObject) Instantiate(explosion, transform.position, transform.rotation);
98 Destroy(explode, 0.8f);
99 GameManager.instance.GameOver();
100 }
101
102 if (collision.tag == "RingGroup") {
103 Destroy(collision.gameObject);
104
105 }
106 }
107
108 public void ScaryPictures() {
109 int rand = Random.Range(0, 5);
110 Vector3 picsPosition = new Vector3(transform.position.x, transform.position.y);
111 GameObject pics = (GameObject)Instantiate(scaryPics[rand], picsPosition, transform.rotation);
112 Destroy(pics, 2.5f);
113 }
114 }